﻿using System.Text;

namespace Infrastructure.Utils
{
    public class MathHelper
    {
        private static Random Random { get; set; } = new Random();

        #region 随机数int
        /// <summary>
        /// 取随机数 取[min,max)中的随机int值
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public static int GetRandom(int min, int max)
        {
            // int32最大值：21亿：2147483647
            return Random.Next(min, max);
        }
        #endregion

        #region 随机验证码-数字
        /// <summary>
        /// 随机验证码-数字[最大长度为10]
        /// </summary>
        /// <param name="length">长度</param>
        public static int RandomCodeInt(int length = 4)
        {
            if (length < 1) length = 1;
            if (length > 10) throw new Exception("最大长度为10");
            return Random.Next((int)Math.Pow(10, length - 1), (int)Math.Pow(10, length) - 1);
        }
        #endregion

        #region 随机验证码-小写字母数字混合
        /// <summary>
        /// 随机验证码-字母数字混合
        /// </summary>
        /// <param name="length">长度</param>
        public static string RandomCode(int length = 4)
        {
            // 除去易混淆的字符0o,g9q,l1I
            var substitute = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "r", "s", "t", "u", "v", "w", "x", "y", "z", "2", "3", "4", "5", "6", "7", "8" };
            var sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(substitute[GetRandom(0, substitute.Length)]);
            }
            return sb.ToString();
        }
        #endregion

        /// <summary>
        /// 19>=位编码生成：前缀+15位时间码(毫秒)+4位随机数
        /// </summary>
        /// <returns></returns>
        public static string CreateNo(string prefix)
        {
            //listNo.Add(YitIdHelper.NextId().ToString() + Thread.CurrentThread.ManagedThreadId.ToString())
            return prefix + DateTime.Now.ToString("yyyyMMddHHmmsss") + new Random().Next(1000, 9999);
        }
        /// <summary>
        /// GUID:32位字符串 eg:e0a953c3ee6040eaa9fae2b667060e09
        /// </summary>
        /// <returns></returns>
        public static string Guid()
        {
            return System.Guid.NewGuid().ToString("N");
        }

    }
}
